package com.demo.controller;

import com.demo.dto.FindNotWorkByTimeDto;
import com.demo.dto.ShowWorksDto;
import com.demo.service.AssignmentService;
import com.demo.service.UserService;
import com.demo.service.UserStudentAssignmentService;
import com.demo.utils.ResponseResult;
import com.demo.pojo.Assignment;
import com.demo.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author: TLB
 * @Date: 2022/11/12 17:32
 */
@RestController
@RequestMapping("/v1/work")
public class AssignmentController {


    @Autowired
    private AssignmentService assignmentService;
    @Autowired
    private UserService userService;

    @Autowired UserStudentAssignmentService userStudentAssignmentService;

    @PostMapping("/publishWork")
    public ResponseResult publishWork(@RequestBody Assignment assignment, HttpSession session) {

        User user = (User) session.getAttribute("user");
        assignment.setUserId(user.getId());

        return assignmentService.addAssignment(assignment);

    }

    @PostMapping("/showWorks")
    public ResponseResult showWorks(@RequestBody ShowWorksDto dto) throws ParseException {

        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        Date time = dto.getTime();

        String s = format.format(time);
        time=format.parse(s);
        dto.setTime(time);

        return userService.showWorks(dto);


    }

    @PostMapping("/findNotWorkByTime")
    public ResponseResult findNotWorkByTime(@RequestBody FindNotWorkByTimeDto dto){
        return userStudentAssignmentService.findNotWorkByTime(dto);
    }
}
